﻿var PagesObject = function (objPageContainer) {
    /*
        The Object that handles the page numbers
    */
    this.root = objPageContainer;// $('#PagesContainer');
    this.dom = this.root[0];
    this.clickedCall = null;
    this.totalOnPage = 15;
    this.selectedPageIndex = 0

    var This = this
		, totalAmmountOfNews = 0
		, isVisible = false
		, Pevious = $('<a>')
		, Next = $('<a>')
		, PagesContainer = $('<td>')
		, FlickData = { //holds the farious data required for the flick to work
		    x: 0
			, y: 0
			, hasMoved: false
			, timer: null
			, maxFlickTime: 1000
			, hasTimed: false
		}
    ;

    /********************************************************************************
	These variables are commented out, because they need to be added using prototype.
	---------------------------------------------------------------------------------
	
	this.strPrevious = 'Previous';
	this.strNext = 'Next';
		
	********************************************************************************/

    function _init() {
        This.root.animate({
            opacity: 0,
            height: 0
        }, 0);

        var body = document.createElement('tbody')
			, table = document.createElement('table')
			, row = document.createElement('tr')
			, td = document.createElement('td')
        ;

        if (typeof This.strPrevious != 'string')
            This.strPrevious = 'Previous';
        if (typeof This.strNext != 'string')
            This.strNext = 'Next';

        Pevious.addClass('Pages-Arrow').text('<<')
        Next.addClass('Pages-Arrow').text('>>')

        //Pevious.attr('title', This.strPrevious);
        //Next.attr('title', This.strNext);

        if (typeof This.dom.ontouchstart != 'object') {
            Next.mouseover(function () {
                Hint.show(This.strNext);
            });
            Next.mouseout(function () {
                Hint.hide();
            });
            Pevious.mouseover(function () {
                Hint.show(This.strPrevious);
            });
            Pevious.mouseout(function () {
                Hint.hide();
            });
        }

        table.className = 'Pages';
        table.appendChild(body);
        body.appendChild(row);

        td.appendChild(Pevious[0]);
        td.className = 'Pages-ArrowContainer';
        row.appendChild(td);

        row.appendChild(PagesContainer[0]);

        td = document.createElement('td');
        td.className = 'Pages-ArrowContainer';
        td.appendChild(Next[0]);

        row.appendChild(td);

        Pevious.click(previousPage);
        Next.click(nextPage);

        $(document).keydown(function (e) {
            switch (e.which) {
                case 37: //arrow left
                    (Utility.isRTL ? nextPage : previousPage)();
                    //previousPage();
                    break;
                case 39: //arrow right
                    (Utility.isRTL ? previousPage : nextPage)();
                    //nextPage();
                    break;
            }
        });

        if (typeof This.dom.ontouchstart == 'object') {
            var obj = $(document);
            obj.on('touchstart', function (e) {

                if (e.originalEvent.touches.length > 1) {
                    return;
                }
                FlickData.x = Utility.Mouse.x;
                FlickData.y = Utility.Mouse.y;
                FlickData.hasMoved = false;
                FlickData.hasTimed = false;
                FlickData.timer = setTimeout(function () {
                    FlickData.hasTimed = true;
                }, FlickData.maxFlickTime);
            });
            obj.on('touchmove', function (e) {
                if (e.originalEvent.touches.length > 1) {
                    return;
                }
                FlickData.hasMoved = true;
            });
            obj.on('touchend', function (e) {
                if (typeof FlickData.timer == 'number') {
                    clearTimeout(FlickData.timer);
                }
                if (!FlickData.hasMoved || FlickData.hasTimed || e.originalEvent.touches.length > 1) {
                    return;
                }

                FlickData.x -= Utility.Mouse.x;
                FlickData.y -= Utility.Mouse.y;

                if (Math.abs(FlickData.x) > Math.abs(FlickData.y) && Math.abs(FlickData.x) > 200 && Math.abs(FlickData.y) < 100) {

                    if (FlickData.x < 0) {
                        (Utility.isRTL ? nextPage : previousPage)();
                        //previousPage();
                    } else {
                        (Utility.isRTL ? previousPage : nextPage)();
                        //nextPage();
                    }
                }
            });
        }

        This.root.append(table);
    }
    function nextPage() {
        selectPage(This.selectedPageIndex + 1);
        Utility.blur()
    }
    function previousPage() {
        selectPage(This.selectedPageIndex - 1);
        Utility.blur()
    }

    this.setPages = function (_totalAmmountOfNews, _totalOnPage, isPartialReset) {
        /*
            Set the pages object up an re-calcualates the pages
            _totalAmmountOfNews - [int] the total ammount of news
            _totalOnPage - [int] the total ammount of news on one page
            isPartialReset - [bool] if set to TRUE then the current pages will simply fade out, while with the full reset i.e. is set to FALSE the pages will also slide out
        */
        totalAmmountOfNews = _totalAmmountOfNews;
        this.totalOnPage = _totalOnPage;

        if (typeof isPartialReset != 'boolean')
            isPartialReset = false;

        this.root.stop(true, false);
        this.selectedPageIndex = 0;

        if (isVisible) {
            var oldHeight = isPartialReset ? this.root.height() : 0;

            this.root.animate({
                opacity: 0,
                height: oldHeight + 'px'
            }, null, null, function () {
                isVisible = false
                makePageMarkers(oldHeight);
            });
        } else {
            makePageMarkers(0);
        }
    };
    this.hide = function (isPartialHide) {

        isVisible = false;
        this.selectedPageIndex = 0;

        if (!isPartialHide) {
            Utility.Effects.hide(this);
        } else {
            this.root.stop(true).fadeOut();
        }

        Pevious.fadeOut();
        Next.fadeOut();
    };

    function selectPage(index) {

        var maxPages = Math.floor(totalAmmountOfNews / This.totalOnPage);

        if (index < 0)
            index = 0;
        if (index > maxPages)
            index = maxPages;
        if (This.selectedPageIndex == index)
            return;

        This.selectedPageIndex = index;
        makePageMarkers();
        if (typeof This.clickedCall == 'function') {
            This.clickedCall(false, false);
        }
    }

    function makePageMarkers(oldHeight) {
        if (typeof oldHeight != 'number')
            oldHeight = 0;

        PagesContainer.empty();

        var i, maxPages = Math.ceil(totalAmmountOfNews / This.totalOnPage),
			dom, intHeight;

        if (maxPages > 1) {
            if (This.selectedPageIndex == 0) {
                Pevious.fadeOut();
                Next.fadeIn();
            } else if (This.selectedPageIndex == maxPages - 1) {
                Pevious.fadeIn();
                Next.fadeOut();
            } else {
                Pevious.fadeIn();
                Next.fadeIn();
            }
        } else {
            Pevious.fadeOut();
            Next.fadeOut();
        }

        for (i = 0; i < maxPages; i++) {

            if (This.selectedPageIndex == i) {
                dom = document.createElement('span');
                dom.className = "Pages-SelectedPageMarker Pages-PageMarker";
            } else {
                dom = document.createElement('a');
                dom.href = "#";
                dom.intPageIndex = i + 0;
                dom.onclick = function () {
                    selectPage(this.intPageIndex);
                };
                dom.className = "Pages-PageMarker";
            }

            if (i) {
                PagesContainer.append(document.createTextNode(' '));
            }
            dom.appendChild(document.createTextNode(Utility.getArabicNumbers(i + 1)));
            PagesContainer.append(dom);
        }

        if (maxPages < 2) {
            This.hide();
            return;
        }

        if (!isVisible) {
            Utility.Effects.show(This);
        }
    }

    _init();
};